""" AdminPermission Model """

from masoniteorm.models import Model
import datetime
from masoniteorm.collection import Collection


class AdminPermission(Model):
    """AdminPermission Model"""

    @classmethod
    def generate_log(cls, title, uri, pid, path):
        """
        生成日志
        @param title:
        @param uri:
        @param pid:
        @param path:
        @return: None
        """
        created_at = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        cls.create({
            'title': title,
            'uri': uri,
            'pid': pid,
            'path': path,
            'created_at': created_at,
            'updated_at': created_at
        })

    @classmethod
    def generate_path(cls, pid):
        """
        生成 path
        @param pid: admin_permissions.id
        @return:
        """
        admin_permission = cls.where('id', pid).first()
        path = '0'
        if admin_permission is not None:
            path = "/" + str(admin_permission.pid)
        return path

    @classmethod
    def get_title_of_pid(cls):
        """
        获取所有 pid=0 的 id
        <class 'list'>
        {1: '菜单', 2: '系统参数'}
        """
        ids = cls.where('pid', 0).get()
        return Collection(ids).pluck('title', 'id').all()

    @classmethod
    def get_all_pid(cls):
        """
        获取访问权限的 title
        @return [{'id': 1, 'title': '系统设置'}, {'id': 2, 'title': '菜单'}]
        """
        permissions = cls.where('pid', 0).get()
        data = []
        if permissions is not None:
            for permission in permissions:
                data.append({'id': permission.id, 'title': permission.title})
        return data

    @classmethod
    def get_ids(cls):
        """
        获取所有的 id
        @return: [1,2,3]
        """
        admin_permissions = cls.get()
        ids = []
        if admin_permissions is not None:
            for admin_permission in admin_permissions:
                ids.append(admin_permission.id)
        return ids

    @classmethod
    def get_permission(cls):
        data = []
        permissions = cls.where('pid', 0).get()
        if permissions is None:
            return data

        for permission in permissions:
            # print(permission.id, permission.title)
            permissions2 = cls.where('pid', permission.id).get()
            children = []
            if permissions2 is not None:
                for index, permission2 in enumerate(permissions2):
                    # print(permission2.uri, permission2.title)
                    children.append({
                        'title': "{0}({1})".format(permission2.title, permission2.uri),
                        'field': 'title',
                        'checked': False,
                        'id': permission2.id
                    })

            data.append({
                'id': permission.id,
                'title': permission.title,
                'field': 'title',
                'checked': False,
                'spread': False,
                'children': children
            })
        """
        PHP 代码示例
        array:2 [
           0 => array:4 [
                "id"        => 1
                "title"     => "测试菜单"
                'field'     => 'title'
                'checked'   => false
                'spread'    => true
                "children"  => array:4 [
                    0 => array:3 [
                        "id"        => 4
                        "title"     => "列表(uri)"
                        "pid"       => 1
                        'field'     => 'title'
                        'checked'   => false
                    ]
                ]
          ]
          1 => array:3 [
              "id"        => 2
              "title"     => "测试菜单3"
              'checked'   => false
              'spread'    => true
              "children"  => []
          ]
        ]
        """
        return data
